package com.yxt.common.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;

public class ImageUtil {
	public static BitmapDrawable zoomPicture(Context context, int resid, int screenWidth,
			int screenHeight, String x, String y, String defaultZoom) {
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),resid);
		return zoomPicture(context,bitmap,screenWidth,screenHeight, x, y, defaultZoom);
	}

	public static BitmapDrawable zoomPicture(Context context, Bitmap bitmap, int screenWidth,
			int screenHeight, String x, String y, String defaultZoom) {
		// 获取这个图片的宽和高
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) screenWidth) / width;
//		float scaleHeight = ((float) screenHeight) / height;
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		matrix.postScale(Float.parseFloat(defaultZoom), Float.parseFloat(defaultZoom));
		Bitmap tempMap = Bitmap.createBitmap(bitmap, 0, 0, width, height,	matrix, true);
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleWidth);
		// 创建新的图片
		Bitmap resizedBitmap;
		if((tempMap.getWidth() - Integer.parseInt(x) > screenWidth) && (tempMap.getHeight() - Integer.parseInt(y) > screenHeight))
			resizedBitmap = Bitmap.createBitmap(tempMap, Integer.parseInt(x), Integer.parseInt(y), screenWidth, screenHeight, matrix, true);
		else
			resizedBitmap = Bitmap.createBitmap(tempMap, 0, 0, tempMap.getWidth(), tempMap.getHeight(), matrix, true);

		BitmapDrawable drawable = new BitmapDrawable(resizedBitmap);
		drawable.setTileModeXY(TileMode.CLAMP, TileMode.CLAMP);
		drawable.setDither(true);
		return drawable;
	}
}
